<template>
  <div>
    <el-form ref="searchForm" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item label="礼品名称">
            <el-input v-model="sd.searchName" size="small" placeholder="礼品名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-search" @click="searchGift">查询</el-button>
          <el-button class="mt_5" type="primary" size="mini" icon="el-icon-plus" @click="addGift">添加</el-button>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="giftObj.list" border style="width: 100%">
      <el-table-column type="expand" width="50">
        <template v-slot="props">
          <ImageUpload onlyShow :is-show-tip="false" :entityId="props.row.id" :entityTypeId="FileEntityType.giftmodel" :key="'ImageUploadListGiftModel' + props.row.id"></ImageUpload>
        </template>
      </el-table-column>
      <el-table-column label="礼品名称" prop="name" width="200"></el-table-column>
      <el-table-column label="礼品分类" prop="typename" width="200"></el-table-column>
      <el-table-column label="礼品规格" prop="guige" width="200"></el-table-column>
      <el-table-column label="礼品品牌" prop="pinpai" width="150"></el-table-column>
      <el-table-column label="描述" prop="descption"></el-table-column>
      <el-table-column label="操作" width="200">
        <template v-slot="scope">
          <el-button size="mini" type="primary" @click="editGift(scope.row.id)">修改</el-button>
          <el-popconfirm confirmButtonText='确定' cancelButtonText='取消'
                         icon="el-icon-info" iconColor="red"
                         title="确定删除数据吗?" @confirm="deleteGift(scope.row.id)">
            <el-button style="margin-left: 10px" slot="reference" size="mini" type="danger">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination v-if="giftObj.total>sd.pageSize" class="center" background layout="total, prev, pager, next"
                   @current-change="loadGift" :total="giftObj.total"
                   :current-page.sync="sd.pageNum" :page-size="sd.pageSize">
    </el-pagination>
    <el-dialog :title="popupTitle" :visible.sync="entityDetailVisible" append-to-body :close-on-click-modal="false" width="80%">
      <giftDetail v-if="entityDetailVisible" :visible.sync="entityDetailVisible" :entityId="popupEntityId"></giftDetail>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import giftDetail from "./giftDetail";
import FileEntityType from "../../../utils/FileEntityType";
export default {
  components:{giftDetail},
  props:{
    typeId : {type : Number, default: 0}
  },
  data() {
    return {
      FileEntityType: FileEntityType,
      sd: {
        typeId: this.typeId,
        searchName:"",
        pageNum: 1,
        pageSize: 10
      },
      giftObj: {},

      entityDetailVisible : false,
      popupTitle : '',
      popupEntityId : 0,
    }
  },
  mounted() {
    this.loadGift()
    this.$EventBus.$on("giftadd", () => {
      this.sd.pageNum = 1;
      this.loadGift();
    });
    this.$EventBus.$on("giftedit", () => {
      this.loadGift();
    });
  },
  beforeDestroy() {
    this.$EventBus.$off("giftadd")
    this.$EventBus.$off("giftedit")
  },
  methods: {
    searchGift(){
      this.sd.pageNum = 1;
      this.loadGift();
    },
    loadGift() {
      request({
        url: '/gift/gift/listgift',
        data: this.sd
      }).then(res => {
        const { data } = res
        this.giftObj = data
      })
    },
    addGift(){
      this.popupEntityId = 0;
      this.popupTitle= "新增礼品"
      this.entityDetailVisible= true
    },
    editGift(id){
      this.popupEntityId= id;
      this.popupTitle= "修改礼品"
      this.entityDetailVisible= true
    },
    deleteGift(id){
      request({
        url: '/gift/gift/deletegift',
        data: {id : id}
      }).then(res => {
        if(res.code === '200') {
          this.$message.success(res.msg)
          this.loadGift();
        }
      })
    }
  }
}
</script>
